/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.agh.air.bpel2bpmn;

import java.awt.Component;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DropTargetEvent;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import javax.swing.TransferHandler;
import javax.swing.TransferHandler.TransferSupport;
import org.apache.log4j.Logger;

/**
 *
 * @author Grzesiek
 */

class Bpel2BpmnTransferHandler extends TransferHandler {

    private static final long serialVersionUID = 1L;

    private final Logger logger = Logger.getLogger(Class.class);
    @Override
    public boolean canImport(TransferSupport support) {
        return (support.isDrop() && support.isDataFlavorSupported(DataFlavor.javaFileListFlavor));
    }

    @Override
    public boolean importData(TransferSupport support) {
        if (!canImport(support)) {
            return false;
        }

        try {
            @SuppressWarnings("unused")
            List<File> files = (List<File>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
 
            File file = files.get(0);
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            DataInputStream dis = null;
            String bpelInput = "";

            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                dis = new DataInputStream(bis);

                byte ch = 0;
                try {
                    while (true) { // exception deals catches EOF
                        ch = dis.readByte();
                        System.out.print((char) ch);
                        bpelInput = bpelInput + (char) ch;
                        System.out.flush();
                    }
                } catch (EOFException eof) {
                    App.inputArea.setText(bpelInput);
                    App.start(bpelInput);
                }

                fis.close();
                bis.close();
                dis.close();

            } catch (FileNotFoundException e) {
                logger.error(e, e);
            } catch (IOException e) {
                logger.error(e, e);
            }
            cancelDnD(support);

            return true;
        } catch (Exception exc) {
            logger.error(exc, exc);
        }

        return false;
    }

    private void cancelDnD(TransferSupport support) {
        /*TransferSupport.setDNDVariables(Component component, DropTargetEvent event)
        Call setDNDVariables(null, null) to free the component.
         */
        try {
            Method m = support.getClass().getDeclaredMethod("setDNDVariables", new Class[]{Component.class, DropTargetEvent.class});
            m.setAccessible(true);
            m.invoke(support, null, null);
        } catch (Exception e) {
        }
    }
}